package com.sky.web.admin;

import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;


    // 员工登录
    @PostMapping("/login")
    public Result login(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        //1. 调用service登录
        Employee employee = employeeService.login(employeeLoginDTO);

        //2. 制作token
        Map<String, Object> claims = new HashMap<>();
        claims.put("empId",employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecret(), jwtProperties.getAdminTtl(), claims);
        // 返回vo
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .name(employee.getName())
                .token(token)
                .userName(employee.getUsername()).build();

        //3.返回结果
        return Result.success(employeeLoginVO);
    }

    // 员工退出
    @PostMapping("/logout")
    public Result logout(){

        return Result.success();
    }


    /**
     * 员工分页
     * @param employeePageQueryDTO
     * @return
     */
    //1 接受请求参数
    @GetMapping("page")
    public Result findByPage(EmployeePageQueryDTO employeePageQueryDTO){

        //2 调用Service 分页查询
        PageBean pageBean = employeeService.findByPage(employeePageQueryDTO);

        //3 返回分页结果
        return Result.success(pageBean);
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping  //1 接收参数
    public Result save(@RequestBody Employee employee){
        //2 调用Service
        employeeService.save(employee);

        //3 返回结果
        return Result.success();
    }

    /**
     *回显员工  (id 查询)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Long id){

        //调用Service
        Employee employee = employeeService.findById(id);
        return Result.success(employee);
    }


    /**
     * 修改员工
     * @param employee
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Employee employee){//1 接收参数
        //2 调用Service
        employeeService.update(employee);

        //3 返回结果
        return Result.success();
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public Result startOrStop(Long id,@PathVariable("status") Integer status){ //1 接收参数
        //2 封装employee并调用service
        Employee employee = Employee.builder()
                .id(id)
                .status(status)
                .build();
        //调用service
        employeeService.update(employee);

        //返回结果
        return Result.success();
    }
}
